package com.ruoyi.project.cms.content.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.content.domain.Content;
import com.ruoyi.project.cms.content.domain.ContentVo;
import com.ruoyi.project.cms.content.service.ICmsContentService;
import com.ruoyi.project.cms.type.domain.Type;
import com.ruoyi.project.cms.type.service.ICmsTypeService;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 黄药师
 * @date 2021-01-30 15:25
 * @desc 巨梯学院 http://www.jutixueyuan.com
 */
@Controller
@RequestMapping("/cms/content")
public class ContentController extends BaseController {

    private String prefix = "/cms/content";

    @Autowired
    private ICmsContentService cmsContentService;

    @Autowired
    private ICmsTypeService cmsTypeService;

    /**
     * 页面跳转
     *
     * @return
     */
    @RequiresPermissions("cms:content:view")  //权限
    @GetMapping
    public String content() {
        return prefix + "/content";
    }

    @RequiresPermissions("cms:content:list")
    @RequestMapping("list")
    @ResponseBody
    public TableDataInfo list(Content content) {

        // 分页
        startPage();
        // 查询数据
        List<ContentVo> list = cmsContentService.selectContentList(content);
        // 返回 TableDataInfo

        return getDataTable(list);
    }

    @GetMapping("add")
    public String add(ModelMap modelMap) {

        Type type = new Type();
        List<Type> types = cmsTypeService.selectTypeList(type);

        System.out.println("types = " + types);

        modelMap.addAttribute("opts",types);
        return prefix + "/add";
    }

    @RequiresPermissions("cms:content:add")
    @ResponseBody
    @PostMapping("add")
    public AjaxResult add(Content content) {
        return toAjax(cmsContentService.insertContent(content));
    }


    @RequestMapping("checkContentTitleUnique")
    @ResponseBody
    public String checkContentTitleUnique(Content content){
        return cmsContentService.selectContentTitleUnique(content);
    }

    @RequiresPermissions("cms:content:edit")
    @GetMapping("edit/{contentId}")
    public String edit(@PathVariable Integer contentId, Model model){

        // 01 拿到 content
        Content content = cmsContentService.selectContentById(contentId);

        //02 拿到所有的typeName
        Type type = new Type();
        List<Type> types = cmsTypeService.selectTypeList(type);
        model.addAttribute("opts",types);

        //03 页面跳转发送数据
        model.addAttribute("content",content);

        return  prefix + "/edit";
    }

    @RequiresPermissions("cms:content:edit")
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(Content content){
        //修改的content
        return toAjax(cmsContentService.updateContent(content));
    }

    @RequiresPermissions("cms:type:remove")
    @PostMapping("remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        try
        {
            return toAjax(cmsContentService.deleteConTentByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
}
